package com.tools;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;


public class NodeView extends View {
    /**
     * 背景画笔
     */
    private Paint bgPaint;
    /**
     * 前景画笔
     */
    private Paint forePaint;
    /**
     * 选中画笔
     */
    private Paint selectPaint;
    /**
     * 文字 title 未选中画笔
     */
    private Paint titleUnselectPaint;
    /**
     * 文字 node 未选中画笔
     */
    private Paint nodeUnselectPaint;
    /**
     * 背景颜色
     */
    private int bgColor = Color.parseColor("#CCCCCC");
    /**
     * 前景颜色
     */
    private int foreColor = Color.parseColor("#F43F3F");
    /**
     * 默认高度
     */
    private int defaultHeight;
    /**
     * 节点文字 上
     */
    private List<String> titleList;
    private List<Rect> titleBounds;
    /**
     * 节点文字 下
     */
    private List<String> nodeList;
    private List<Rect> nodeBounds;
    /**
     * 节点圆的半径
     */
    private int radius;
    /**
     * 文字和节点进度条的top
     */
    private int marginTop;
    /**
     * 两个节点之间的距离
     */
    private int dividWidth;
    /**
     * 选中位置
     */
    private int selectIndex;

    public NodeView(Context context, AttributeSet attrs) {
        super(context,attrs);
        init(context);
    }

    private void init(Context context) {
        radius = SizeUtils.dp2px(context,7);
        defaultHeight = SizeUtils.dp2px(context,30);
        marginTop = SizeUtils.dp2px(context,5);

        bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        bgPaint.setColor(bgColor);
        bgPaint.setStyle(Paint.Style.FILL);

        forePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        forePaint.setColor(foreColor);
        forePaint.setStyle(Paint.Style.FILL);

        nodeUnselectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        nodeUnselectPaint.setColor(bgColor);
        nodeUnselectPaint.setTextSize(SizeUtils.sp2px(context,10));

        titleUnselectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        titleUnselectPaint.setColor(bgColor);
        titleUnselectPaint.setTextSize(SizeUtils.sp2px(context,10));

        selectPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        selectPaint.setColor(foreColor);
        selectPaint.setTextSize(SizeUtils.sp2px(context,10));
    }

    /**
     * 设置数据
     */
    public void setTitleList(List<String> titleData){
        if(titleData != null){
            titleList = titleData;
        }
        //测量文字所占用的空间
        measureTitleText();
    }

    /**
     * 设置数据
     */
    public void setNodeList(List<String> nodeDatas){
        if(nodeDatas != null){
            nodeList = nodeDatas;
        }
        //测量文字所占用的空间
        measureNodeText();
    }

    /**
     * 设置选中位置
     */
    public void setSelectIndex(int selectIndex){
        this.selectIndex = selectIndex;
        invalidate();
    }

    /**
     * 测量文字 上的长宽
     */
    private void measureTitleText(){
        titleBounds = new ArrayList<>();
        for (int i = 0; i < titleList.size(); i++) {
            Rect mBound = new Rect();
            titleUnselectPaint.getTextBounds(titleList.get(i), 0, titleList.get(i).length(), mBound);
            titleBounds.add(mBound);
        }
    }

    /**
     * 测量文字 下的长宽
     */
    private void measureNodeText(){
        nodeBounds = new ArrayList<>();
        for (int i = 0; i < nodeList.size(); i++) {
            Rect mBound = new Rect();
            nodeUnselectPaint.getTextBounds(nodeList.get(i),0,nodeList.get(i).length(),mBound);
            nodeBounds.add(mBound);
        }
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(nodeList == null || nodeList.isEmpty()){
            return;
        }
        //两个圆之间相隔距离
        dividWidth = (getWidth()-radius*2)/(nodeList.size() - 1);

        //绘制title文字
        for (int i = 0; i < titleList.size(); i++) {
            int currentTextWidth = nodeBounds.get(i).width();
            if (i==0){
                if (i==selectIndex){
                    canvas.drawText(titleList.get(i), 0, nodeBounds.get(i).height(), selectPaint);
                }else if(i>selectIndex) {
                    canvas.drawText(titleList.get(i), 0, nodeBounds.get(i).height(), titleUnselectPaint);
                }
            }else if (i==nodeList.size()-1){
                if (i==selectIndex){
                    for (int j = 0; j <= i; j++) {
                        if(j == 0){
                            canvas.drawText(titleList.get(j), 0, nodeBounds.get(j).height(), selectPaint);
                        }else if(j == i){
                            canvas.drawText(titleList.get(j), getWidth() - currentTextWidth, nodeBounds.get(j).height(), selectPaint);
                        }else{
                            canvas.drawText(titleList.get(j), radius + j * dividWidth - currentTextWidth / 2, nodeBounds.get(j).height(), selectPaint);
                        }
                    }
                }else if(i>selectIndex) {
                    canvas.drawText(titleList.get(i), getWidth() - currentTextWidth, nodeBounds.get(i).height(), titleUnselectPaint);
                }
            }else {
                if (i==selectIndex){
                    for (int j = 0; j <= i; j++) {
                        if(j>0){
                            canvas.drawText(titleList.get(j), radius + j * dividWidth - currentTextWidth / 2, nodeBounds.get(j).height(), selectPaint);
                        }else{
                            canvas.drawText(titleList.get(j), 0, nodeBounds.get(j).height(), selectPaint);
                        }
                    }
                }else if(i>selectIndex) {
                    canvas.drawText(titleList.get(i), radius + i * dividWidth - currentTextWidth / 2, nodeBounds.get(i).height(), titleUnselectPaint);
                }
            }
        }

        bgPaint.setStrokeWidth(radius/2);
        //绘制灰色的背景线条
        canvas.drawLine(radius,radius*2 + marginTop + nodeBounds.get(2).height()/2,getWidth()-radius,radius*2 + marginTop + nodeBounds.get(2).height()/2,bgPaint);

        //画节点圆
        forePaint.setStrokeWidth(radius/2);
        for (int i = 0; i < nodeList.size(); i++) {
            if(i == selectIndex){
                for (int j = 0; j <= i; j++) {
                    canvas.drawCircle(radius+ j * dividWidth, radius*2 + marginTop + nodeBounds.get(2).height()/2, radius , forePaint);
                    canvas.drawLine(radius,radius*2 + marginTop + nodeBounds.get(i).height()/2,j*dividWidth,radius*2 + marginTop + nodeBounds.get(i).height()/2,forePaint);
                }
            }else if(i>selectIndex){
                canvas.drawCircle(radius + i * dividWidth, radius*2 + marginTop + nodeBounds.get(2).height()/2, radius, bgPaint);
            }
        }

        //绘制node文字
        for (int i = 0; i < nodeList.size(); i++) {
            int currentTextWidth = nodeBounds.get(i).width();
            if (i==0){
                if (i==selectIndex){
                    canvas.drawText(nodeList.get(i), 0, radius*4 + marginTop*3 + nodeBounds.get(i).height()/2, selectPaint);
                }else if(i>selectIndex) {
                    canvas.drawText(nodeList.get(i), 0, radius*4 + marginTop*3 + nodeBounds.get(i).height()/2, nodeUnselectPaint);
                }
            }else if (i==nodeList.size()-1){
                if (i==selectIndex){
                    for (int j = 0; j <= i; j++) {
                        if(j == 0){
                            canvas.drawText(nodeList.get(j), 0, radius*4 + marginTop*3 + nodeBounds.get(i).height()/2, selectPaint);
                        }else if(j == i){
                            canvas.drawText(nodeList.get(j), getWidth() - currentTextWidth, radius*4 + marginTop*3 + nodeBounds.get(i).height()/2, selectPaint);
                        }else{
                            canvas.drawText(nodeList.get(j), radius + j * dividWidth - currentTextWidth / 2, radius*4 + marginTop*3 + nodeBounds.get(i).height()/2, selectPaint);
                        }
                    }
                }else if(i>selectIndex) {
                    canvas.drawText(nodeList.get(i), getWidth() - currentTextWidth, radius*4 + marginTop*3 + nodeBounds.get(i).height()/2, nodeUnselectPaint);
                }
            }else {
                if (i==selectIndex){
                    for (int j = 0; j <= i; j++) {
                        if(j>0){
                            canvas.drawText(nodeList.get(j), radius + j * dividWidth - currentTextWidth / 2, radius*4 + marginTop*3 + nodeBounds.get(i).height()/2, selectPaint);
                        }else{
                            canvas.drawText(nodeList.get(j), 0, radius*4 + marginTop*3 + nodeBounds.get(i).height()/2, selectPaint);
                        }
                    }
                }else if(i>selectIndex) {
                    canvas.drawText(nodeList.get(i), radius + i * dividWidth - currentTextWidth / 2, radius*4 + marginTop*3 + nodeBounds.get(i).height()/2, nodeUnselectPaint);
                }
            }
        }
    }
}
